/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-05 16:29:00
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanViewOrder;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.DbException;
import zyy.util.Bus;

public class FrmRiderHub extends JDialog implements ActionListener {
  private static final long serialVersionUID = -1879264831856356064L;

  // private BeanViewOrder currentVOrder = Link.currentVOrder;
  // private BeanViewOrder selectVOrder = null;
  private JPanel list = new JPanel();
  private Object tblVOrderTitles[] = BeanViewOrder.tblTitles;
  private Object tblVOrderData[][];
  DefaultTableModel tabVOrderModel = new DefaultTableModel();
  private JTable dataTableVOrder = new JTable(tabVOrderModel);
  List<BeanViewOrder> allVOrder = null;

  private BeanViewOrder curVOrder;

  private JButton choose = new JButton("抢单");
  private JButton run = new JButton("送单");
  private JButton fail = new JButton("放弃订单");
  private JButton exit = new JButton("退出");
  private JPanel btns = new JPanel();

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.choose) {
      try {
        Bus.orderManager.riderChoose(this.curVOrder);
        this.reloadVOrderTable();
      } catch (DbException | BusinessException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      }
    } else if (e.getSource() == this.run) {
      try {
        Bus.orderManager.riderRun(this.curVOrder);
        this.reloadVOrderTable();
      } catch (DbException | BusinessException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      }
    } else if (e.getSource() == this.fail) {
      try {
        Bus.orderManager.riderFail(this.curVOrder);
        this.reloadVOrderTable();
      } catch (DbException | BusinessException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      }
    } else if (e.getSource() == this.exit) {
      this.setVisible(false);
    }
  }

  public void reloadVOrderTable() {
    try {
      allVOrder = Bus.orderManager.load_V_All(null);
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblVOrderData = new Object[allVOrder.size()][tblVOrderTitles.length];
    for (int i = 0; i < allVOrder.size(); i++) {
      for (int j = 0; j < tblVOrderTitles.length; j++)
        tblVOrderData[i][j] = allVOrder.get(i).getCell(j);
    }
    tabVOrderModel.setDataVector(tblVOrderData, tblVOrderTitles);
    // 限定列宽必须在reload里写死
    dataTableVOrder.getColumnModel().getColumn(0).setPreferredWidth(55);
    dataTableVOrder.getColumnModel().getColumn(1).setPreferredWidth(60);
    dataTableVOrder.getColumnModel().getColumn(2).setPreferredWidth(70);
    dataTableVOrder.getColumnModel().getColumn(3).setPreferredWidth(280);
    dataTableVOrder.getColumnModel().getColumn(4).setPreferredWidth(255);
    dataTableVOrder.getColumnModel().getColumn(5).setPreferredWidth(140);
    dataTableVOrder.getColumnModel().getColumn(6).setPreferredWidth(210);
    dataTableVOrder.getColumnModel().getColumn(7).setPreferredWidth(100);
    this.dataTableVOrder.validate();
    this.dataTableVOrder.repaint();
    System.out.println("成功重载 View_User_Orders 信息");
  }

  public FrmRiderHub() {
    // 基础 JDialog 设置
    {
      // Link.fUserGo = this;
      this.setTitle("外卖助手 骑手 查看订单");
      this.setSize(1318, 838);
      this.setLocationRelativeTo(null);
      // this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          // System.exit(0);
          Bus.fUserOrders.setVisible(false);
        }
      });
      this.setVisible(true);
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.list, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
      list.setLayout(new BorderLayout());
      list.add(new JScrollPane(this.dataTableVOrder), BorderLayout.CENTER);

      // 表格列宽自适应(其实这一行，没觉得有什么作用)
      // 一定不能选 AUTO_RESIZE_OFF 就好了
      dataTableVOrder.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.choose);
      this.btns.add(this.run);
      this.btns.add(this.fail);
      this.btns.add(this.exit);
      this.choose.addActionListener(this);
      this.run.addActionListener(this);
      this.fail.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.choose);
      Bus.jButtonZYYStyle(this.run);
      Bus.jButtonZYYStyle(this.fail);
      Bus.jButtonZYYStyle(this.exit);
    }
    // JTable 监听设置
    {
      Bus.currentVOrder = this.curVOrder;
      this.dataTableVOrder.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmRiderHub.this.dataTableVOrder.getSelectedRow();
          if (i < 0) {
            return;
          }
          FrmRiderHub.this.reloadVOrderTable();
          FrmRiderHub.this.dataTableVOrder.setRowSelectionInterval(i, i);
          {
            FrmRiderHub.this.curVOrder = FrmRiderHub.this.allVOrder.get(i);
            System.out.println("选中订单：" + FrmRiderHub.this.curVOrder);
          }
        }
      });
    }
    // 内容初始化
    {
      this.reloadVOrderTable();
      System.out.println("AAA 共有列数：" + dataTableVOrder.getColumnModel().getColumnCount());
    }
  }
}